package com.baosight.sczx.zm.cs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.ef.ui.tree.TreeConstants;
import com.baosight.iplat4j.ef.ui.tree.TreeService;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCS;
import com.baosight.sczx.zm.cs.domain.ZMCSSystemModule;

/**
 * <p>Title: ServiceZMCS01.java</p>
 * <p>Description: 系统模块,构造树</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2010-12-16 下午01:29:29  张小飞  Create
 *          2011-04-21 下午14:17     严洪浩  Modify 修改Integer类型的字段为VarChar2
 *          						  原来0代表停用,现在直接保存停用等...
 *</pre>
 */
public class ServiceZMCS01 extends TreeService {
	
	private String fdKeyId = "SYSTEMMODULE00000000000000000001";
	private String fdParentId = "SYSTEMMODULE00000000000000000000";
	
	/**
	 * 系统模块树结构：构造查询树节点的对应子节点
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11上午10:10:53 张小飞
	 * </p>
	 *
	 */

	@Override
	public List<?> getChildNodes(String args) {
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put(TreeConstants.TREE_NODE, args);
		_argMap.put("fdStatus", "在用");
		
		List<?> childNodes = dao.query("ZMCSSystemModule.treeChildNodes", _argMap);
		return childNodes;
	}

	/**
	 * 系统模块树结构：得到根节点数据
	 * <p>Function: getTopNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11上午10:11:05 严洪浩
	 * </p>
	 *
	 */

	@Override
	public List<Map<String, String>> getTopNodes() {
		List<Map<String, String>> _topNodes = new ArrayList<Map<String, String>>();
		String systemName = SystemSettingCS.getInstance().getSystemName();//得到系统名称
		int i = 0;
		
		Map<String, String> _tempMap = new HashMap<String, String>();
		_tempMap.put("label", fdKeyId);
		_tempMap.put("text", systemName);		
		_tempMap.put("leaf", "0");
		_tempMap.put("fdModuleLevel", "0");
		
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("fdKeyId", fdKeyId);
		List<?> checkTopNodes = dao.query("ZMCSSystemModule.treeTopNodes", _tempMap);//ROOT节点数据校验
		
		if(checkTopNodes != null && checkTopNodes.size() >= 1){
			ZMCSSystemModule resultMap = (ZMCSSystemModule)checkTopNodes.get(0);
			String fdModuleName = resultMap.getFdModuleName();			
			if(fdModuleName == null || !fdModuleName.equals(systemName)) {
				_tempMap = new HashMap<String, String>();
				_tempMap.put("fdKeyId", fdKeyId);
				dao.delete("ZMCSSystemModule.delete", _tempMap);//名称不一致，删除数据
			}
			else {
				i++;
			}
		}
		
		if(i == 0) {
			Map<String, Object> _argMap = new HashMap<String, Object>();
			_argMap.put("fdKeyId", fdKeyId);//<!-- FD_KEY_ID -->
			_argMap.put("fdSerialNumber", 0);
			_argMap.put("fdModuleNo", "ZM");
			_argMap.put("fdModuleName", systemName);
			_argMap.put("fdModuleLevel", 0);
			_argMap.put("fdParentId", fdParentId);
			_argMap.put("fdNature", "标准版");
			_argMap.put("fdType", "基础");
			_argMap.put("fdStatus", "在用");
			_argMap.put("fdPackage", "com.baosight.sczx.zm");
			dao.insert("ZMCSSystemModule.treeInsertTopNodes", _argMap);
		}

		return _topNodes;
	}

	/**
	 * 叶子列定义
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11上午10:11:21 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();

		EiColumn eiColumn = new EiColumn("label");
		eiColumn.setDescName("FD_KEY_ID");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("text");
		eiColumn.setDescName("模块名称");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("leaf");
		eiColumn.setDescName("是否叶子");
		eiColumn.setType(EiConstant.COLUMN_TYPE_NUMBER);
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdModuleLevel");
		eiColumn.setDescName("模块级别");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}

}
